package v1

import (
	"gitee.com/pangxianfei/framework/http/controller"
	"gitee.com/pangxianfei/library/response"
	"gitee.com/pangxianfei/multiapp/multiapp"
	"gitee.com/pangxianfei/multiapp/sysmodel"

	"tmaic/app/user/http/request"
	"tmaic/app/user/services"
)

type UserController struct {
	controller.BaseController
}

// PostInfo 获取当前登录用户
func (this *UserController) PostInfo() *response.JsonResult {
	return response.JsonData(multiapp.Auth.User(this.Context))
}

// PostStore 创建登陆帐号
func (this *UserController) PostStore() *response.JsonResult {
	var UserStore request.UserRequset
	if newErr, returnData := this.Validation.Json(this.Context, &UserStore, UserStore.Messages()); newErr != nil {
		return response.JsonDataError(returnData)
	}
	if _, createErr := multiapp.Instance.CreateLoginAccount(this.Context, UserStore.UserName, UserStore.Mobile, UserStore.Password); createErr != nil {
		return response.JsonErrorMsg(createErr.Error())
	}
	return response.JsonCreateSucces("")
}

// PostEditBy 修改用户资料
func (this *UserController) PostEditBy() *response.JsonResult {
	return response.JsonSuccess()
}

// PostSetUsername 设置用户名
func (this *UserController) PostSetUsername() *response.JsonResult {
	return response.JsonSuccess()
}

// PostSetEmail 设置邮箱
func (this *UserController) PostSetEmail() *response.JsonResult {
	return response.JsonSuccess()
}

// PostUpdatePassword 修改密码
func (this *UserController) PostUpdatePassword() *response.JsonResult {
	var UpdatePassword request.UpdatePasswordRequest
	if err, returnData := this.Validation.Json(this.Context, &UpdatePassword, UpdatePassword.Messages()); err != nil {
		return this.JsonFail(returnData)
	}
	if updateErr := services.PlatformAdminService.UpdateAdminPassword(this.Context, UpdatePassword); updateErr != nil {
		return this.JsonFail(updateErr.Error())
	}
	return this.JsonUpdateSuccess("密码更新成功")
}

// PostUpdateStatus 启用或者禁用登陆帐号
func (this *UserController) PostUpdateStatus() *response.JsonResult {
	var UserStatusRequset request.UserStatusRequset

	if err, returnData := this.Validation.Json(this.Context, &UserStatusRequset, UserStatusRequset.Messages()); err != nil {
		return this.JsonFail(returnData)
	}

	if updateErr := services.PlatformAdminService.PostUpdateStatus(this.Context, UserStatusRequset); updateErr != nil {
		return this.JsonFail(updateErr.Error())
	}

	return this.JsonUpdateSuccess("")
}

// PostDb 用户应用  - 测试获取应用DB对象
func (this *UserController) PostDb() *response.JsonResult {

	db := multiapp.DB.Initiation(this.Context)
	if db == nil {
		return this.JsonDataError("对象为空")
	}
	var admin sysmdel.PlatformAdmin
	db.Debug().First(&admin)
	return this.JsonQueryData(admin)
}
